package org.zjx.operation;

/**
 * 重复执行操作
 */
public class RepeatOperation implements WindowOperation<Void> {
    private final WindowOperation<?> operation;
    private final int times;
    private final long interval;

    public RepeatOperation(WindowOperation<?> operation, int times) {
        this(operation, times, 0);
    }

    public RepeatOperation(WindowOperation<?> operation, int times, long interval) {
        this.operation = operation;
        this.times = times;
        this.interval = interval;
    }

    @Override
    public Void execute(OperationExecutor executor) {
        for (int i = 0; i < times; i++) {
            operation.execute(executor);
            if (interval > 0 && i < times - 1) {
                executor.delay(interval);
            }
        }
        return null;
    }
}